import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-27
 * Time: 15:26
 */
class Imp implements Comparator<Integer> {

    public int  compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}
public class test {
    // 获取数据中前k个最小的数据
    public static int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if (k == 0) return ret;
        //创建大堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Imp());
        for (int i = 0; i < k; i++) maxHeap.offer(arr[i]);
        for (int i = k; i < arr.length; i++) {
            int top = maxHeap.peek();
            if (top > arr[i]){
                maxHeap.poll();
                maxHeap.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) ret[i] = maxHeap.poll();
        return ret;
    }
    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] arr = { 27,15,19,18,28,34,65,49,25,37 };
        testHeap.createHeap(arr);
        testHeap.show();
        testHeap.offer(80);
        testHeap.show();
        testHeap.poll();
        testHeap.show();
        int[] ret = smallestK(arr, 3);
        for (int x : ret) System.out.print(x + " ");
        System.out.println();
        testHeap.heapSort();
        testHeap.show();
    }
}
